package com.zhima.controller;

import com.zhima.config.LocalTaskProperties;
import com.zhima.entity.LocalTask;
import com.zhima.mapper.LocalTaskMapper;
import com.zhima.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("local-task")
public class LocalTaskController {
    @Autowired
    private TaskService taskService;

    @Autowired
    private LocalTaskMapper localTaskMapper;
    @Autowired
    private LocalTaskProperties localTaskProperties;

    @PostMapping("/publish/{taskType}")
    public boolean publishTask(@PathVariable("taskType") String taskType, @RequestBody List<String> businessKeys) {
        return taskService.publishTask(taskType, businessKeys);
    }

    @GetMapping("/all")
    public List<LocalTask> all() {
        return localTaskMapper.selectList(null);
    }

    @GetMapping("/count")
    public Integer count() {
        System.out.println(localTaskProperties);
        List<LocalTask> localTasks = localTaskMapper.selectList(null);
        System.out.println(localTasks);
        return localTasks.size();
    }

    @GetMapping("/issueTasks/{taskType}/{limit}")
    public List<LocalTask> issueTasks(@PathVariable("taskType") String taskType, @PathVariable("limit") Integer limit) {
        return taskService.dispatchTasks(taskType, limit);
    }

    @PostMapping("/completeSubTask/{taskId}/{subTaskIndex}")
    public boolean completeSubTaskWithSubTaskIndex(@PathVariable("taskId") Integer taskId,
                                                   @PathVariable("subTaskIndex") Integer subTaskIndex) {
        taskService.completeSubTask(taskId, subTaskIndex, null);
        return true;
    }

    @PostMapping("/completeSubTask/{taskId}")
    public boolean completeSubTaskWithoutSubTaskIndex(@PathVariable("taskId") Integer taskId) {
        taskService.completeSubTask(taskId, null);
        return true;
    }

    @PostMapping("/startSubtask/{taskId}/{subTaskIndex}")
    public boolean startSubTaskWithSubTaskIndex(@PathVariable("taskId") Integer taskId,
                                                @PathVariable("subTaskIndex") Integer subTaskIndex) {
        return taskService.startSubTask(taskId, subTaskIndex);
    }

    @PostMapping("/startSubtask/{taskId}")
    public boolean startSubTaskWithoutSubTaskIndex(@PathVariable("taskId") Integer taskId) {
        return taskService.startSubTask(taskId);
    }
}
